<?php

if(!defined('IN_SYS')) exit('Access Denied');

class theme_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$themes = ymcms::get_themes();
		foreach($themes as $k => $v){
			if(is_file(ROOT."/templates/{$k}/preview.png")){
				$themes[$k]['preview'] = "/templates/{$k}/preview.png";
			}else{
				$themes[$k]['preview'] = "/static/common/images/nopic.png";
			}
		}
		require self::tpl('theme');
	}

	public function updateTplEngine(){
		$theme = gp('theme');
		$engine = gp('engine');
		$themeDir = ROOT.'/templates/'.$theme;
		$configXml = $themeDir.'/yumingcms.xml';
		if(is_dir($themeDir)){
			if(is_file($configXml)){
				//$xml = simplexml_load_file($configXml);
				$xmlstring = file_get_contents($configXml);
				$xml = new SimpleXMLElement($xmlstring);
			}else{
				$xmlstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>\r\n<engine/>\r\n</root>";
				$xml = new SimpleXMLElement($xmlstring);
			}
			$xml->engine = $engine;
			file_put_contents($configXml, $xml->asXML());

			$tplcache = DATA.'/tplcache/'.$theme."/*.htm";
			$tpls = glob($tplcache);
			foreach($tpls as $tpl){
				@unlink($tpl);
			}
		}
		echo 'success';
	}

}


